package com.interesting.client.UI.setting;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.interesting.R;
import com.interesting.base.core.BaseFragment;
import com.interesting.base.http.HttpUtil;
import com.interesting.base.lazylist.ImageLoader;
import com.interesting.base.utils.Constant;
import com.interesting.base.utils.SQLiteUtil;
import com.interesting.client.UI.commonActivity.UIUtil;
import com.interesting.client.application.AppHolder;
import com.interesting.client.bean.VersionInfo;
import com.interesting.client.support.update.UpdateUtil;

/** 设置主界面 fragment */

public class MoreFragment extends BaseFragment {

	private TextView opinion, update, about, clear;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.setting_set_fragment, container, false);
		opinion = (TextView) v.findViewById(R.id.set_fragment_opinion);// 意见反馈
		update = (TextView) v.findViewById(R.id.set_fragment_update);// 检查更新
		clear = (TextView) v.findViewById(R.id.set_fragment_clear);// 清除缓存
		about = (TextView) v.findViewById(R.id.set_fragment_about);// 关于
		return v;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		OnClickListener listener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				String tag = null;
				switch (v.getId()) {

				case R.id.set_fragment_opinion: // 意见反馈
					tag = "opinion";
					getSherlockActivity().setTitle(getResources().getString(R.string.opinion));
					break;
				case R.id.set_fragment_update: // 检查更新
					UIUtil.showMessageDialog(getFragmentManager(), "检测中...");
					new UpdateAsyncTask().execute();
					break;
				case R.id.set_fragment_clear: // 检查更新
					UIUtil.showMessageDialog(getFragmentManager(), "清扫中...");
					new ClearAsyncTask().execute();
					break;
				case R.id.set_fragment_about: // 关于
					tag = "about";
					getSherlockActivity().setTitle(getResources().getString(R.string.about));

					break;
				}
				if (tag != null) {
					((MoreActivity) getSherlockActivity()).switchfragment(tag);
				}
			}
		};
		opinion.setOnClickListener(listener);
		update.setOnClickListener(listener);
		about.setOnClickListener(listener);
		clear.setOnClickListener(listener);
	}

	// 检查更新 的 后台异步
	class UpdateAsyncTask extends AsyncTask<String, Void, JSONObject> {

		@Override
		protected JSONObject doInBackground(String... params) {
			Map<String, String> request = new HashMap<String, String>();
			request.put("act", "getNewVersion");
			return HttpUtil.parseResult(HttpUtil.post(request));
		}

		@Override
		protected void onPostExecute(JSONObject result) {
			try {
				if (result != null) {

					JSONObject vision = result.getJSONObject("userData");
					VersionInfo info = new VersionInfo();
					info.setApkName(vision.getString("apkname"));
					info.setApkRemark(vision.getString("apkremark"));
					info.setApkSize(vision.getString("apksize"));
					info.setApkURL(Constant.IMGPATH + "/" + vision.getString("apkurl"));
					info.setApkVersion(vision.getString("apkversion"));
					UpdateUtil.checkVersion(getSherlockActivity(), getSherlockActivity().getSupportFragmentManager(),
							info);
				} else {
					UIUtil.showToast(getSherlockActivity(), "此版本为最新版");
				}
			} catch (JSONException e) {
				UIUtil.showToast(getSherlockActivity(), "版本号异常");
				e.printStackTrace();
			} finally {
				UIUtil.hideDialog(getFragmentManager());
			}
		}
	}

	// 检查更新 的 后台异步
	class ClearAsyncTask extends AsyncTask<String, Void, String> {
		SQLiteUtil sqlite = AppHolder.getInsatnce().dbAdapter;
		ImageLoader il = AppHolder.getInsatnce().imageLoader;

		@Override
		protected String doInBackground(String... params) {
			sqlite.getWritableDatabase().delete(SQLiteUtil.TABLE_JOKE, null, null);
			il.clearCache();
			return "success";
		}

		@Override
		protected void onPostExecute(String result) {
			try {
				if (result.equals("success"))
					UIUtil.showToast(getSherlockActivity(), "清除成功");
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				UIUtil.hideDialog(getFragmentManager());
			}

		}
	}

}
